package com.ruoyi.system.controller;

import java.util.List;

import com.ruoyi.web.controller.tool.JsonResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.JingfeiDept;
import com.ruoyi.system.service.IJingfeiDeptService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * jingfeideptController
 * 
 * @author ruoyi
 * @date 2024-07-11
 */
@Controller
@RequestMapping("/system/jingfeidept")
public class JingfeiDeptController extends BaseController
{
    private String prefix = "system/jingfeidept";

    @Autowired
    private IJingfeiDeptService jingfeiDeptService;

    @RequiresPermissions("system:jingfeidept:view")
    @GetMapping()
    public String dept()
    {
        return prefix + "/dept";
    }

    /**
     * 查询jingfeidept列表
     */
    @RequiresPermissions("system:jingfeidept:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(JingfeiDept jingfeiDept)
    {
        startPage();
        List<JingfeiDept> list = jingfeiDeptService.selectJingfeiDeptList(jingfeiDept);
        return getDataTable(list);
    }

    /**
     * 导出jingfeidept列表
     */
    @RequiresPermissions("system:jingfeidept:export")
    @Log(title = "jingfeidept", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(JingfeiDept jingfeiDept)
    {
        List<JingfeiDept> list = jingfeiDeptService.selectJingfeiDeptList(jingfeiDept);
        ExcelUtil<JingfeiDept> util = new ExcelUtil<JingfeiDept>(JingfeiDept.class);
        return util.exportExcel(list, "jingfeidept数据");
    }

    /**
     * 新增jingfeidept
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存jingfeidept
     */
    @RequiresPermissions("system:jingfeidept:add")
    @Log(title = "jingfeidept", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(JingfeiDept jingfeiDept)
    {
        return toAjax(jingfeiDeptService.insertJingfeiDept(jingfeiDept));
    }

    /**
     * 修改jingfeidept
     */
    @RequiresPermissions("system:jingfeidept:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        JingfeiDept jingfeiDept = jingfeiDeptService.selectJingfeiDeptById(id);
        mmap.put("jingfeiDept", jingfeiDept);
        return prefix + "/edit";
    }

    /**
     * 修改保存jingfeidept
     */
    @RequiresPermissions("system:jingfeidept:edit")
    @Log(title = "jingfeidept", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(JingfeiDept jingfeiDept)
    {
        return toAjax(jingfeiDeptService.updateJingfeiDept(jingfeiDept));
    }

    /**
     * 删除jingfeidept
     */
    @RequiresPermissions("system:jingfeidept:remove")
    @Log(title = "jingfeidept", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(jingfeiDeptService.deleteJingfeiDeptByIds(ids));
    }

    @Log(title = "单位经费发放列表", businessType = BusinessType.UPDATE)
    @PostMapping("/pici_list")
    @ResponseBody
    public JsonResult pici_list(@RequestParam("piciname")String ppPiciName, HttpServletRequest request) {

        try {
            HttpSession session = request.getSession();
            String deptid = session.getAttribute("deptid").toString();
            return JsonResult.getSuccessResult(jingfeiDeptService.findDWByPiciNameAndDeptId(deptid,ppPiciName));
        } catch (Exception e) {
            logger.error("jingfeidept -> pici_list: "+e.getMessage());
            return JsonResult.getErrorResult("经费批次列表查询失败");
        }
    }
}
